﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using imaginecup.util;
using Imaginecup.sprite;
using Imaginecup.util;
using Imaginecup.util.collision;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.factory;
using Imaginecup.sprite.changeSourceSize;
using Microsoft.Xna.Framework.Input;

namespace Imaginecup.gameState.choiceMap
{
    public class IconMan:ISourceInfoClass
    {
        private EntityInfo m_Info;
        private ISprite m_Sprite;

        private StateFactoryID m_StateID;
        private StateStageManID m_StageManID;

        private bool m_bClear;
        private bool m_bSelect = false;

        private EntityInfo m_SourceInfo;

        public IconMan(StateStageManID stageManID , Vector2 iconSize)
        {
            m_StageManID = stageManID;
            m_bClear = false;
            m_SourceInfo = new EntityInfo();
            m_SourceInfo.mSize = iconSize;
        }
                
        public void SetStateFactoryID(StateFactoryID factoryID)
        {
            m_StateID = factoryID;
        }
        public void SetSprite(ISprite sprite)
        {
            m_Sprite = sprite;
        }

        public void SetEntityInfo(EntityInfo info)
        {
            m_Info = info;
            m_SourceInfo.mSize = m_Info.mSize;
        }

        public  void Release()
        {
            m_Sprite.Release();

        }

        public bool IsSelect()
        {
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();

            if (input.IsOneClickUp() && Collision.CollisionByPoint(m_Info, input.Position))
            {
                if( m_StateID == StateFactoryID.State_Malaria || m_StateID == StateFactoryID.State_WaterGame )
                {
                    IControlStageMan controlStageMan = (IControlStageMan)StateFactory.GetStateFactory().GetObject(m_StateID);
                    controlStageMan.SetStageManID(m_StageManID);
                }

                return true;
            }
            return false;
        }

        public void Update( )
        {
            IMouseInputHandler mouseInputHandler = MouseInputHandler.GetInputHandler();

            if (mouseInputHandler.MouseState.LeftButton == ButtonState.Pressed && Collision.CollisionByPoint(m_Info, mouseInputHandler.Position))
            {
                m_bSelect = true;
            }
            else
                m_bSelect = false;
        }
        public void Draw(GameTime gameTime , SpriteBatch sb)
        {
            m_Sprite.Draw(sb, m_Info);
        }

        public IGameState GetGameState()
        {
            return (IGameState)StateFactory.GetStateFactory().GetObject(m_StateID);
        }

        public bool GetClear()
        {
            return m_bClear;
        }
        public void SetClear()
        {
            m_bClear = true;
        }

        public EntityInfo GetSourceInfo()
        {
            if (!m_bSelect)
            {
                m_SourceInfo.mPosition = Vector2.Zero;
            }
            else
            {
                m_SourceInfo.mPosition = new Vector2(m_SourceInfo.mSize.X, 0);
            }
            return m_SourceInfo;
        }
    }
}
